#include "xtea.h"
#include <string.h>

static const uint32_t ROUNDS = 32;
static const uint32_t DELTA = (uint32_t) 0x9E3779B9;

void xtea_encrypt (uint8_t const * const key, 
									 uint8_t const * const plaintextblock, 
									 uint8_t * const ciphertextblock)
{
	uint32_t v0, v1;
	uint32_t *k, *c;
	uint32_t sum;
	uint32_t i;
	memset ((void *) ciphertextblock, 0, 8);
	sum = 0;
	v0 = ((uint32_t *) plaintextblock) [0];
	v1 = ((uint32_t *) plaintextblock) [1];
	k = (uint32_t *) key;
	c = (uint32_t *) ciphertextblock;
	for (i = 0; i < ROUNDS; i ++)
	{
		v0 = v0 + ((v1 + ((v1 << 4) ^ (v1 >> 5))) ^ (sum + k [sum & 3]));
		sum = sum + DELTA;
		v1 = v1 + ((v0 + ((v0 << 4) ^ (v0 >> 5))) ^ (sum + k [(sum >> 11) & 3]));
	}
	c [0] = v0;
	c [1] = v1;
}

